package com.xo.mall.server.impl;

import com.github.pagehelper.page.PageMethod;
import com.xo.common.Result.CommonPage;
import com.xo.common.Result.ResultResponse;
import com.xo.common.constant.NumberConstant;
import com.xo.common.enums.ResultEnum;
import com.xo.common.pojo.dto.req.GetPageProductListReqDto;
import com.xo.common.pojo.dto.req.PmsProductCreateReqDto;
import com.xo.common.pojo.entity.PmsProductEntity;
import com.xo.common.pojo.vo.PageProductListVo;
import com.xo.common.pojo.vo.ProductInfoByProductIdVo;
import com.xo.common.until.PageUtil;
import com.xo.mall.convert.PmsProductConvert;
import com.xo.mall.mapper.PmsProductMapper;
import com.xo.mall.server.PmsProductService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PmsProductServiceImpl implements PmsProductService {

    @Resource
    private PmsProductMapper pmsProductMapper;

    @Override
    public ResultResponse<String> createProduct(PmsProductCreateReqDto pmsProductCreateReqDto) {
        PmsProductEntity pmsProductEntity = PmsProductConvert.INSTANCE.PmsProductCreateDtoTOPmsProductEntity(pmsProductCreateReqDto);
        int count = pmsProductMapper.createProduct(pmsProductEntity);
        if (count < NumberConstant.INT_ONE) {
            return ResultResponse.fail(ResultEnum.CREATE_FAIL.getMessage());
        }
        return ResultResponse.success(ResultEnum.CREATE_SUCCESS.getMessage());
    }

    @Override
    public ResultResponse<ProductInfoByProductIdVo> getProductInfoByProductId(Integer productId) {
        PmsProductEntity pmsProductEntity = pmsProductMapper.selectProductInfoByProductId(productId);
        ProductInfoByProductIdVo productInfoByProductIdVo = PmsProductConvert.INSTANCE.PmsProductEntityTOFindProductInfoByProductIdVo(pmsProductEntity);
        return ResultResponse.success(productInfoByProductIdVo);
    }

    @Override
    public ResultResponse<CommonPage<PageProductListVo>> getPageProductList(GetPageProductListReqDto getPageProductListReqDto) {
        // 开启分页
        PageMethod.startPage(getPageProductListReqDto.getPageNum(), getPageProductListReqDto.getPageSize());
        List<PageProductListVo> pageProductListVoList = pmsProductMapper.selectPageProductList(getPageProductListReqDto);
        return ResultResponse.success(PageUtil.getPageList(pageProductListVoList));
    }


}
